import { BasicColumn, FormSchema } from '@/components/Table';
import { h, ref } from 'vue';
import { Tag } from 'ant-design-vue';
import { ywUserSelectList } from '@/api/heng/ywManage';
import { ApiSelect } from '@/components/Form';
import util from '@/utils/util';

import { useMessage } from '@/hooks/web/useMessage';
import Ainput from '@/views/bookManage/components/account/Ainput.vue';
import { VxeFormItemProps, VxeGridPropTypes } from '@/components/VxeTable';
import { changeEnd } from 'codemirror';

export function openIsReturn(record: isReturn) {
  console.log('--openIsReturn ', record);
}

type CheckedType = boolean | string | number;
// BasicColumn[]

// VxeColumnPropTypes.CellRender

/**
 * VxeTable 列配置数组，定义了业务资金列表的表格结构
 * 包含以下主要列:
 * - 单据来源编号、类型、收款日期等基础信息
 * - 营销组、金额等业务数据
 * - 订单抽成比例及金额计算
 * - 接单人、跟单人、业务经理等角色的抽成配置
 * - 项目组抽成及审批状态
 *
 * 支持复选框选择、过滤、编辑等功能
 * 订单抽成比例变更时会自动计算各角色的抽成金额
 */
export const vxeTableColumns: VxeGridPropTypes.Columns = [
  {
    type: 'checkbox',
    title: '',
    field: 'seq',
    width: 40,
  },
  {
    title: '单据来源编号(订单标识号)',
    field: 'orderNo',
    showOverflow: 'title',
    filters: [{ data: '' }],
    filterRender: {
      name: 'VxeInput',
    },
    width: 130,
  },
  {
    title: '来源类型',
    field: 'type',
    showOverflow: 'title',
    filters: [{ data: '' }],
    align: 'center',

    filterRender: {
      name: 'VxeInput',
    },
    width: 100,
  },
  {
    title: '收款日期',
    field: 'payTime',
    showOverflow: 'title',
    align: 'center',
    width: 140,
    formatter: ({ cellValue, row, column }) => {
      let val = '';
      if (!cellValue) {
        return val;
      }
      let arr = cellValue.split(' ');
      val = `${arr[0]}`;
      return val;
    },
  },
  {
    title: '币种',
    field: 'currencyType',
    showOverflow: 'title',
    align: 'center',
    width: 80,
  },
  {
    title: '客户编号',
    field: 'customerNo',
    showOverflow: 'title',
    align: 'center',
    width: 80,
  },
  {
    title: 'ms营销组',
    field: 'salesmanDeptName0',
    showOverflow: 'title',
    filters: [{ data: '' }],
    filterRender: {
      name: 'VxeInput',
    },
    width: 100,
  },
  {
    title: '金额(人民币)',
    field: 'totalAllPrice',
    showOverflow: 'title',
    width: 100,
    slots: {
      footer: 'footer_totalAllPrice',
    },
  },

  {
    title: '订单子备注',
    field: 'childRemark',
    showOverflow: 'title',
    align: 'center',
    width: 100,
  },
  {
    title: '抽成比例',
    field: 'orderCommissionRate0',
    showOverflow: 'title',
    formatter: ({ cellValue, row, column }) => {
      let val = '';
      if (!cellValue) {
        return val;
      }
      val = `${cellValue ? cellValue + '%' : cellValue + '%'}`;
      return val;
    },
    width: 100,
  },
  {
    title: '抽成金额',
    field: 'orderCommissionPrice0',
    showOverflow: 'title',
    width: 100,
    slots: {
      footer: 'footer_orderCommissionPrice',
    },
  },
  // {
  //   title: '单号',
  //   field: 'orderNo',
  //   align: 'center',
  //   width: 140,
  //
  // },
  {
    title: '接单人',
    field: 'receiverUserName0',
    align: 'center',
    filters: [{ data: '' }],
    filterRender: {
      name: 'VxeInput',
    },
    // editRender: { name: 'VxeInput' },

    width: 100,
  },
  // {
  //   title: '接单人比例',
  //   field: 'receiverUserRate0',
  //   formatter: ({ cellValue, row, column }) => {
  //     let val = '';
  //     if (!cellValue) {
  //       return val;
  //     }
  //     val = `${cellValue ? cellValue + '%' : cellValue + '%'}`;
  //     return val;
  //   },

  //   align: 'center',
  //   width: 100,
  // },
  {
    title: '接单人抽成金额',
    field: 'receiverUserPrice0',
    align: 'center',
    slots: {
      footer: 'footer_receiverUserPrice',
    },
    width: 100,
  },

  {
    title: '业务经理',
    field: 'manageUserName0',
    align: 'center',
    width: 70,
  },
  {
    title: '经理抽成',
    field: 'manageUserPrice0',
    align: 'center',
    width: 90,
  },
  {
    title: '项目组',
    field: 'xiangMuGroupName0',
    align: 'center',
    width: 70,
  },
  {
    title: '项目组抽成',
    field: 'xiangMuGroupPrice0',
    align: 'center',
    slots: {
      footer: 'footer_xiangMuGroupPrice',
    },
    width: 90,
  },
  {
    title: '团建基金',
    field: 'jiJingName0',
    width: 80,
  },
  {
    title: '团建金额',
    field: 'jiJingPrice0',
    slots: {
      footer: 'footer_jiJingPrice',
    },
    width: 80,
  },

  {
    title: 'ms收款单号',
    field: 'queRenNo',
    width: 130,
  },

  {
    title: 'ms收款状态',
    field: 'stateName0',
    align: 'center',
    width: 80,
  },
  // {
  //   width: 120,
  //   title: '操作',
  //   align: 'center',
  //   field: '',
  //   slots: { default: 'action' },
  //   fixed: 'right',
  // },
];

export const vxeTableFormSchema: VxeFormItemProps[] = [
  {
    span: 5,
    field: 'orderNoLike',
    title: '查询',
    itemRender: {
      props: {
        placeholder: '来源单据编号',
      },
      name: 'AInput',
    },
  },
  // {
  //   span: 5,
  //   field: 'state0',
  //   title: 'ms审批状态',
  //   itemRender: {
  //     name: 'ASelect',
  //     defaultValue: 9999,
  //     props: {
  //       options: [
  //         { label: '已审批', value: 9999 },
  //         { label: '审批中', value: 0 },
  //         { label: '需修改', value: -1 },
  //       ],
  //     },
  //   },
  // },
  // {
  //   span: 5,
  //   field: 'userNameLogin',
  //   title: '模拟登录人员',
  //   itemRender: {
  //     props: {
  //       placeholder: '人员名称',
  //     },
  //     name: 'AInput',
  //   },
  // },
  {
    span: 5,
    field: 'dateArr',
    title: '收款日期区间',
    itemRender: {
      props: {
        valueFormat: 'YYYY-MM-DD',
        format: 'YYYY-MM-DD',
      },
      name: 'ARangePicker',
    },
  },
  {
    span: 5,
    itemRender: {
      name: 'VxeButtonGroup',
      options: [
        { type: 'reset', content: '重置' },
        { type: 'submit', content: '查询', status: 'primary' },
      ],
    },
  },
];

export const rules = {
  mouldNameObj: [{ required: true, message: '费用名称不能为空', trigger: 'change' }],
  materialNo: [
    { required: true, message: '物料编号不能为空', trigger: 'change' },
    { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
  orderNo: [
    { required: true, message: '采购单号不能为空', trigger: 'change' },
    { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
};

var baseFormSchema = [
  {
    field: '',
    label: '',
    component: 'group',
    items: [
      {
        field: 'isHandBoole',
        label: '是否手动输入费用',
        required: true,
        component: 'Switch',
      },
      {
        field: 'mouldNameObj',
        label: '型号',
        required: true,
        component: 'SelectOrderIndex',
        colProps: {
          span: 11,
        },
        show: true,
      },
      {
        field: 'mouldName',
        label: '型号',
        required: false,
        component: 'Input',
        colProps: {
          span: 11,
        },
        show: false,
      },
    ],
  },

  {
    field: '',
    label: '',
    component: 'group',
    items: [
      {
        field: 'materialName',
        label: '物料名称',
        component: 'Input',
        required: true,
        placeholder: '请输入物料名称',
        colProps: {
          span: 13,
          offset: 2,
        },
      },
      {
        field: 'specification',
        label: '物料规格',
        component: 'Input',
        required: true,
        placeholder: '请输入物料规格',
        colProps: {
          span: 13,
          offset: 2,
        },
      },
    ],
  },

  // {
  //   field: '',
  //   label: '',
  //   component:"group",
  //   items:[
  //     {
  //       field: 'materialNo',
  //       label: '物料编号',
  //       component: 'Input',
  //       colProps: {
  //         span: 13, offset: 2
  //       },
  //
  //     },
  //     {
  //       field: 'orderNo',
  //       label: '采购单号',
  //       component: 'Input',
  //
  //       colProps: {
  //         span: 13, offset: 2
  //       },
  //     },
  //   ]
  // },
  {
    field: 'materialNo',
    label: '物料编号',
  },
  {
    field: 'orderNo',
    label: '采购单号',
  },
  {
    field: 'mouldName',
    label: '费用名称',
  },
  {
    field: 'model',
    label: '物料型号',
  },
  {
    field: '',
    label: '',
    component: 'group',
    items: [
      {
        field: 'price',
        label: '金额',
        required: true,
        component: 'InputNumber',
        colProps: {
          span: 13,
          offset: 2,
        },
      },
      {
        label: '供应商',
        field: 'supplierObj',
        colProps: {
          span: 13,
          offset: 2,
        },
        component: 'SelectSupplierIndex',
        required: true,
      },
    ],
  },

  {
    field: 'supplierName',
    label: '供应商名称',
  },
  {
    field: 'materialNos',
    label: '物料虚拟件',
  },

  {
    field: 'isHand',
    label: '是否手动输入',
  },
  // {
  //   label: '费用类型',
  //   field: 'costTypeId',
  //   component: 'ApiSelect',
  //   componentProps: {
  //     api: dictAllListApi,
  //     params:{type:1},
  //     resultField:"data",
  //     labelField: 'name',
  //     valueField: 'id',
  //   },
  //   required: true,
  // },

  {
    field: '',
    label: '',
    component: 'group',
    items: [
      {
        field: 'startDate',
        label: '开始日期',
        required: true,
        component: 'DatePicker',
        componentProps: {
          valueFormat: 'YYYY-MM-DD',
          format: 'YYYY-MM-DD',
        },
        colProps: {
          span: 13,
          offset: 2,
        },
      },
      {
        field: 'endDate',
        label: '结束日期',
        component: 'DatePicker',
        componentProps: {
          valueFormat: 'YYYY-MM-DD',
          format: 'YYYY-MM-DD',
        },
        colProps: {
          span: 13,
          offset: 2,
        },
      },
      // {
      //   label: '人员类型',
      //   field: 'userType',
      //   required: true,
      //   component: 'Select',
      //   colProps: {
      //     span: 11,
      //   },
      //   componentProps: {
      //     options: [
      //       {label: '采购员', value: 1},
      //       {label: '研发制样员', value: 2},
      //
      //     ],
      //   },
      //
      // },
    ],
  },
  {
    field: '',
    label: '',
    component: 'group',
    items: [
      {
        label: '经办人',
        field: 'userName',
        component: 'Ainput',
        rules: [{ required: true }],
        colProps: {
          span: 13,
          offset: 2,
        },
      },
      {
        field: 'remark',
        label: '备注',
        component: 'Input',
        colProps: {
          span: 13,
          offset: 2,
        },
      },
    ],
  },
  {
    field: 'returnCondition',
    label: '',
    component: 'group',
    items: [
      {
        field: 'returnConditionType',
        label: '退款条件类别',
        required: true,
        component: 'Select',
        colProps: {
          span: 11,
        },
        componentProps: {
          options: [
            { label: '一次性(数量)', value: 1 },
            { label: '累计(数量)', value: 2 },
            { label: '一次性(金额)', value: 4 },
            { label: '累计(金额)', value: 5 },
            { label: '不退', value: 3 },
          ],
        },
      },
      {
        field: 'returnConditionNum',
        label: '退款条件数量',
        required: true,
        component: 'InputNumber',
        colProps: {
          span: 11,
        },
      },
    ],
  },
  {
    field: 'totalNum',
    label: '累计下单量',
    component: 'InputNumber',
    placeholder: '该累计下单量将与MS采购单数量相加来对比退款条件数量!',
    show: false,
  },
  {
    field: 'userId',
    label: '经办人id',
  },
];

/**
 * 确认提交审批列表
 */

export const confirmColumns: VxeGridPropTypes.Columns = [
  {
    title: '单据来源编号',
    field: 'orderNo',
    showOverflow: 'title',
    width: 130,
  },
  {
    title: '类型',
    field: 'type',
    showOverflow: 'title',
    align: 'center',
    width: 100,
  },

  {
    title: '金额(人民币)',
    field: 'totalAllPrice',
    showOverflow: 'title',
    width: 100,
    slots: {
      footer: 'footer_totalAllPrice',
    },
  },
  {
    title: '订单抽成金额',
    field: 'orderCommissionPrice0',
    showOverflow: 'title',
    width: 100,
    slots: {
      footer: 'footer_orderCommissionPrice',
    },
  },

  {
    title: '接单人',
    field: 'receiverUserName0',
    align: 'center',
    width: 100,
  },
  {
    title: '接单人抽成金额',
    field: 'receiverUserPrice0',
    align: 'center',
    width: 100,
    slots: {
      footer: 'footer_receiverUserPrice',
    },
  },

  {
    title: '跟单人',
    field: 'merchandiserUserName0',
    align: 'center',
    width: 100,
  },
  {
    title: '跟单人抽成金额',
    field: 'merchandiserUserPrice0',
    align: 'center',
    width: 100,
    slots: {
      footer: 'footer_merchandiserUserPrice',
    },
  },

  {
    title: '业务经理',
    field: 'manageUserName0',
    align: 'center',
    width: 70,
  },
  {
    title: '经理抽成',
    field: 'manageUserPrice0',
    slots: {
      footer: 'footer_manageUserPrice',
    },
    align: 'center',
    width: 90,
  },
  {
    title: '项目组',
    field: 'xiangMuGroupName0',
    align: 'center',
    width: 70,
  },
  {
    title: '项目组抽成',
    field: 'xiangMuGroupPrice0',
    align: 'center',
    width: 90,
    slots: {
      footer: 'footer_xiangMuGroupPrice',
    },
  },
  {
    title: '团建基金',
    field: 'jiJingName0',
    width: 80,
  },
  {
    title: '团建金额',
    field: 'jiJingPrice0',
    width: 80,
    slots: {
      footer: 'footer_jiJingPrice',
    },
  },
];

export const formSchema = baseFormSchema;

export function checkboxCountFn(rows) {
  let totalAllPrice = 0;
  let receiverUserPrice = 0;
  let manageUserPrice = 0,
    orderCommissionPrice = 0,
    introduceUserPrice = 0,
    xiangMuGroupPrice = 0,
    jiJingPrice = 0,
    merchandiserUserPrice = 0;
  for (let r1 in rows) {
    let item = rows[r1];
    if (item.totalAllPrice) {
      totalAllPrice = totalAllPrice + item.totalAllPrice;
    }
    if (item.receiverUserPrice0) {
      receiverUserPrice = receiverUserPrice + item.receiverUserPrice0;
    }
    if (item.manageUserPrice0) {
      manageUserPrice = manageUserPrice + item.manageUserPrice0;
    }
    if (item.orderCommissionPrice0) {
      orderCommissionPrice = orderCommissionPrice + item.orderCommissionPrice0;
    }
    if (item.xiangMuGroupPrice0) {
      xiangMuGroupPrice = xiangMuGroupPrice + item.xiangMuGroupPrice0;
    }
    if (item.introduceUserPrice) {
      introduceUserPrice = introduceUserPrice + item.introduceUserPrice;
    }

    if (item.jiJingPrice0) {
      jiJingPrice = jiJingPrice + item.jiJingPrice0;
    }
    if (item.merchandiserUserPrice0) {
      merchandiserUserPrice = merchandiserUserPrice + item.merchandiserUserPrice0;
    }
  }
  let totalObj = {
    totalAllPrice: totalAllPrice ? totalAllPrice.toFixed(2) : null,
    receiverUserPrice: receiverUserPrice ? receiverUserPrice.toFixed(2) : null,
    manageUserPrice: manageUserPrice ? manageUserPrice.toFixed(4) : null,
    orderCommissionPrice: orderCommissionPrice ? orderCommissionPrice.toFixed(2) : null,
    xiangMuGroupPrice: xiangMuGroupPrice ? xiangMuGroupPrice.toFixed(2) : null,
    jiJingPrice: jiJingPrice ? jiJingPrice.toFixed(2) : null,
    merchandiserUserPrice: merchandiserUserPrice ? merchandiserUserPrice.toFixed(2) : null,
    introduceUserPrice: introduceUserPrice ? introduceUserPrice.toFixed(2) : null,
  };
  return totalObj;
}

export function checkboxCountFn2(rows) {
  let totalAllPrice = 0;
  let receiverUserPrice = 0;
  let manageUserPrice = 0,
    orderCommissionPrice = 0,
    xiangMuGroupPrice = 0,
    jiJingPrice = 0,
    merchandiserUserPrice = 0;
  for (let r1 in rows) {
    let item = rows[r1];
    if (item.totalAllPrice) {
      totalAllPrice = totalAllPrice + item.totalAllPrice;
    }
    if (item.receiverUserPrice) {
      receiverUserPrice = receiverUserPrice + item.receiverUserPrice;
    }
    if (item.manageUserPrice) {
      manageUserPrice = manageUserPrice + item.manageUserPrice;
    }
    if (item.orderCommissionPrice) {
      orderCommissionPrice = orderCommissionPrice + item.orderCommissionPrice;
    }
    if (item.xiangMuGroupPrice) {
      xiangMuGroupPrice = xiangMuGroupPrice + item.xiangMuGroupPrice;
    }
    if (item.jiJingPrice) {
      jiJingPrice = jiJingPrice + item.jiJingPrice;
    }
    if (item.merchandiserUserPrice) {
      merchandiserUserPrice = merchandiserUserPrice + item.merchandiserUserPrice;
    }
  }
  let totalObj = {
    totalAllPrice: totalAllPrice ? totalAllPrice.toFixed(2) : null,
    receiverUserPrice: receiverUserPrice ? receiverUserPrice.toFixed(2) : null,
    manageUserPrice: manageUserPrice ? manageUserPrice.toFixed(2) : null,
    orderCommissionPrice: orderCommissionPrice ? orderCommissionPrice.toFixed(2) : null,
    xiangMuGroupPrice: xiangMuGroupPrice ? xiangMuGroupPrice.toFixed(2) : null,
    jiJingPrice: jiJingPrice ? jiJingPrice.toFixed(2) : null,
    merchandiserUserPrice: merchandiserUserPrice ? merchandiserUserPrice.toFixed(2) : null,
  };
  return totalObj;
}
